from PyQt6.QtWidgets import (QApplication, QMainWindow, QTextEdit, 
                            QFileDialog, QMessageBox, QToolBar, QStatusBar)
from PyQt6.QtGui import QAction, QIcon
from PyQt6.QtCore import Qt
import sys

class Notepad(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("简易记事本")
        self.setGeometry(100, 100, 800, 600)
        
        # 创建文本编辑区
        self.text_edit = QTextEdit()
        self.setCentralWidget(self.text_edit)
        
        # 创建菜单栏
        self.create_menus()
        
        # 创建工具栏
        self.create_toolbar()
        
        # 创建状态栏
        self.statusBar().showMessage("就绪")
    
    def create_menus(self):
        # 文件菜单
        file_menu = self.menuBar().addMenu("文件")
        
        # 新建动作
        new_action = QAction("新建", self)
        new_action.setShortcut("Ctrl+N")
        new_action.triggered.connect(self.new_file)
        file_menu.addAction(new_action)
        
        # 打开动作
        open_action = QAction("打开...", self)
        open_action.setShortcut("Ctrl+O")
        open_action.triggered.connect(self.open_file)
        file_menu.addAction(open_action)
        
        # 保存动作
        save_action = QAction("保存", self)
        save_action.setShortcut("Ctrl+S")
        save_action.triggered.connect(self.save_file)
        file_menu.addAction(save_action)
        
        # 退出动作
        exit_action = QAction("退出", self)
        exit_action.setShortcut("Ctrl+Q")
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)
    
    def create_toolbar(self):
        toolbar = QToolBar("主工具栏")
        self.addToolBar(toolbar)
        
        # 添加工具按钮
        new_btn = QAction(QIcon("new.png"), "新建", self)
        new_btn.triggered.connect(self.new_file)
        toolbar.addAction(new_btn)
        
        open_btn = QAction(QIcon("open.png"), "打开", self)
        open_btn.triggered.connect(self.open_file)
        toolbar.addAction(open_btn)
        
        save_btn = QAction(QIcon("save.png"), "保存", self)
        save_btn.triggered.connect(self.save_file)
        toolbar.addAction(save_btn)
    
    def new_file(self):
        self.text_edit.clear()
        self.statusBar().showMessage("新建文件")
    
    def open_file(self):
        file_name, _ = QFileDialog.getOpenFileName(self, "打开文件")
        if file_name:
            try:
                with open(file_name, 'r') as f:
                    self.text_edit.setText(f.read())
                self.statusBar().showMessage(f"已打开: {file_name}")
            except Exception as e:
                QMessageBox.warning(self, "错误", f"无法打开文件:\n{str(e)}")
    
    def save_file(self):
        file_name, _ = QFileDialog.getSaveFileName(self, "保存文件")
        if file_name:
            try:
                with open(file_name, 'w') as f:
                    f.write(self.text_edit.toPlainText())
                self.statusBar().showMessage(f"已保存: {file_name}")
            except Exception as e:
                QMessageBox.warning(self, "错误", f"无法保存文件:\n{str(e)}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    notepad = Notepad()
    notepad.show()
    sys.exit(app.exec())